package com.yidu.controller;

import com.yidu.domain.BaseMessage;
import com.yidu.domain.BlogsUser;
import com.yidu.domain.vo.BlogsUserVo;
import com.yidu.service.LoginOrRegisterService;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.BaseStream;

/**
 * @author: 肖楚科
 * @date: 2021/1/25
 * @time: 14:06
 * @blog: com.yidu.controller
 * @description:
 */
@Controller
@RequestMapping("/logOrReg")
public class LoginOrRegisterController {
    @Resource
    LoginOrRegisterService loginOrRegisterService;

    /**
     * 登录
     *
     * @param userName
     * @param userPassword
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public BaseMessage login(String userName, String userPassword) {
        //查到有用户则返回成功方法，否则失败
        return loginOrRegisterService.check(userName, userPassword) > 0 ? BaseMessage.success() : BaseMessage.fail();
    }

    /**
     * 上传照片
     * @param file
     * @param request
     * @return 返回layui需要的json，包括照片路径，用于显示预览图
     */
    @RequestMapping("/photo")
    @ResponseBody
    public Map<String,Object> photo(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        //得到文件名
        String fileName = file.getOriginalFilename();
        if (!StringUtils.isEmpty(fileName)) {
            int index = fileName.lastIndexOf('.');
            String hz = fileName.substring(index);
            //使用jdk提供的随机字符串
            String newFileName = UUID.randomUUID().toString() + hz;
            //真实路径
            String realPath = request.getServletContext().getRealPath("/upload");
            File target = new File(realPath + "/" + newFileName);
            try {
                //保存文件
                file.transferTo(target);
            } catch (IOException e) {
                e.printStackTrace();
            }
            map.put("code",0);
            map.put("msg","");
            map.put("data","/upload/" + newFileName);
        }else {
            map.put("code",1);
            map.put("msg","上传失败");
        }
        return map;
    }

    /**
     * 增加修改
     * @param blogsUser
     * @return
     */
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(BlogsUserVo blogsUser){
        return loginOrRegisterService.insertSelective(blogsUser)>0 ? BaseMessage.success():BaseMessage.fail();
    }
//    @Test
//    public void t(){
//        BlogsUser blogsUser = new BlogsUser();
//        blogsUser.setUserRegtime(LocalDate.parse("1970-01-01"));
//        //Date date = new Date(LocalDate.now().toString());
//        System.out.println(blogsUser.getUserRegtime().toEpochDay());
//    }
}
